package com.example.administrator.printoutindemo.http;


import android.content.Context;


import java.util.Map;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import com.jess.arms.base.BaseConstants;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;


/**
 * Created by 山东中和慧明 on 2018/8/1.
 */

public class HttpManager {

    private static final int READ_TIME_OUT = 20;
    private static final int CONNECT_TIME_OUT = 20;
    private static HttpManager instanse = null;
    private ApiService mApiService;
    private Context context;
    private String webservice = "/storeservice.asmx/";

    private HttpManager() {
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(HttpLoggingInterceptor.Logger.DEFAULT);//打印retrofit日志
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        if (SPUtil.getString(context, "duankouhao", "").equals("6677")) {
            webservice = "/StoreService.asmx/";
        }
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .readTimeout(READ_TIME_OUT, TimeUnit.SECONDS)
                .connectTimeout(CONNECT_TIME_OUT, TimeUnit.SECONDS)
                .addInterceptor(loggingInterceptor)
                .build();

        Retrofit retrofit = new Retrofit.Builder()
                .client(okHttpClient)
//                .addConverterFactory(GsonConverterFactory.create())
                .addConverterFactory(ScalarsConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .baseUrl(SPUtil.getString(context, BaseConstants.HTTP_URL, "") + webservice)//Uri.base   http://dezhou.xywuhe.com:8088
                .build();
        mApiService = retrofit.create(ApiService.class);
    }

    public static HttpManager getInstance() {
        if (instanse == null) {
            synchronized (HttpManager.class) {
                if (instanse == null) {
                    return new HttpManager();
                }
            }
        }
        return instanse;
    }

    public void get(String url, Map<String, String> params, Observer<String> subscriber) {
        Observable<String> observable = mApiService.get(url, params);
        call(observable, subscriber);
    }

    private void call(Observable<String> observable, Observer<String> subscriber) {
        observable.subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }

    public void get(String url, Observer<String> subscriber) {
        Observable<String> observable = mApiService.get(url);
        call(observable, subscriber);
    }

    public void post(String url, Map<String, String> params, Observer<String> subscriber) {
        Observable<String> observable = mApiService.post(url, params);
        call(observable, subscriber);
    }


    public void post(String url, Observer<String> subscriber) {
        Observable<String> observable = mApiService.post(url);
        call(observable, subscriber);
    }

    public void upload(String url, Map<String, String> params, MultipartBody.Part file, Observer<String> subscriber) {
        Observable<String> observable = mApiService.upload(url, params, file);
        call(observable, subscriber);
    }
//    public void post(String url, JSONObject json, Subscriber<String> subscriber){
//
//        Observable <String> observable=mApiService.post(url,json);
//        call(observable,subscriber);
//    }


}
